<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="">
        <span>易一健康.资料比鉴</span>
      </div>
      <div>
        <i class="iconfont icon-zhanghu"></i>
        <el-dropdown>
          <span class="el-dropdown-link pointer">
            {{ username }}
            <i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="loginout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
      <!-- <el-button type="info" @click="loginout">退出</el-button> -->
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 左侧边栏 -->
      <el-aside :width="toggleMenu ? '64px' : '200px'">
        <!-- 折叠或展示 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu :default-active="activePath" class="el-menu-vertical-demo" background-color="#333744" text-color="#fff"
          unique-opened :collapse="toggleMenu" :collapse-transition="false">
          <!-- 证型/症状 -->
          <el-submenu :index="'' + item.id" v-for="(item) in menuList" :key="item.id">
            <template slot="title">
              <i class="iconfont icon-zhongxiyijieheke"></i>
              <span>{{ item.menuName }}</span>
            </template>
            <el-menu-item :index="'' + item2.id" v-for="item2 in item.children" :key="item2.id"
              @click="toggleItem(item2)">
              <template slot="title">
                <i class="iconfont icon-ic_juzhengdan"></i>
                <span>{{ item2.menuName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容 -->
      <el-main>
        <div>
          <w_templates :activePath="activePath"></w_templates>
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import w_templates from '../components/template.vue'
export default {
  components: {
    w_templates,
  },
  name: 'index',
  data() {
    return {
      menuName: '',
      navDisabled: false,
      toggleMenu: false,
      activePath: null,
      menuList: [
        {
          menuName: '证型/症状', id: 101, path: '', children: [
            { menuName: '证型', id: 102, path: '' },
            { menuName: '症状', id: 103, path: '' }
          ]
        },
        {
          menuName: '中病/西病', id: 201, path: '', children: [
            { menuName: '中病', id: 202, path: '' },
            { menuName: '西病', id: 203, path: '' }
          ]
        },
        {
          menuName: '成药/草药', id: 301, path: '', children: [
            { menuName: '成药', id: 302, path: '' },
            { menuName: '草药', id: 303, path: '' },
            { menuName: '方剂', id: 304, path: '' }
          ]
        },
      ]
    }
  },
  computed: {
    username() {
      return localStorage.getItem('username')
    },

  },
  created() {
    let path = window.sessionStorage.getItem('activePath')
    if (!path) {
      this.activePath = '' + this.menuList[0].children[0].id
    } else {
      this.activePath = path
    }
  },
  methods: {
    loginout() {
      // 清除sessionStorage中的登录状态
      window.sessionStorage.clear()
      // 跳转到登录页4
      this.$router.push('/login')
    },
    //侧边栏展示收起
    toggleCollapse(flag) {
      this.toggleMenu = !this.toggleMenu
    },
    /**
     * 
     * @param {*} item2 
     */
    toggleItem(item2) {
      this.menuName = item2.menuName
      //缓存菜单激活状态
      window.sessionStorage.setItem('activePath', '' + item2.id)
      this.activePath = '' + item2.id
    }
  }
}
</script>

<style scoped lang="less">
.el-container {
  height: 100%;
}

.el-header {
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #373D41;

  img {
    width: 50px;
    height: 50px;
  }

  div {
    display: flex;
    align-items: center;

    span {
      margin-left: 10px;
    }
  }
}

.pointer {
  cursor: pointer;
}

.el-aside {
  color: #fff;
  background-color: #333744;
}

.el-dropdown {
  color: #fff;
  font-size: 16px;
}

.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 5px;
}

.el-menu {
  border-right: 0px;
}

.toggle-button {
  color: #fff;
  cursor: pointer;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  background: #4a5064;

}
</style>